﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CubicFWNet.Data.Models;
using CubicFWNet.Data.SystemManager;
using CubicFWNet.Data.ViewModal;

namespace CubicFWNet.Application.SystemManager
{
    public class CustomerApp
    {
        private CustomerRepository _customerRepository;
        private UserRepository _userRepository;

        public CustomerApp()
        {
            _customerRepository = new CustomerRepository();
            _userRepository = new UserRepository();
        }

        /// <summary>
        /// 获取客户[供应商]列表
        /// </summary>
        /// <param name="name"></param>
        /// <param name="salerID"></param>
        /// <param name="type">1:客户   2:供应商</param>
        /// <returns></returns>
        public IEnumerable<Customer> GetList(string name, Guid salerID, int type)
        {
            Guid id = new Guid();
            return _customerRepository.IQueryable(a => (a.Name.Contains(name) || a.ShortName.Contains(name)) && (salerID == id || a.UserId == salerID) && (a.Type == type || type == 0)).OrderByDescending(a => a.CreateTime);
        }

        /// <summary>
        /// 获取客户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Customer GetInfo(Guid id)
        {
            return _customerRepository.FindEntity(a => a.Id == id);
        }

        /// <summary>
        /// 添加客户信息
        /// </summary>
        /// <param name="cus"></param>
        /// <returns></returns>
        public int Insert(Customer cus)
        {
            return _customerRepository.Insert(cus);
        }

        /// <summary>
        /// 更新客户信息
        /// </summary>
        /// <param name="cus"></param>
        /// <returns></returns>
        public int Update(Customer cus)
        {
            return _customerRepository.Update(cus);
        }

        /// <summary>
        /// 获取一个岗位下的全部用户
        /// </summary>
        /// <param name="roleID"></param>
        /// <returns></returns>
        public IEnumerable<User> GetUserList(Guid roleID)
        {
            return _userRepository.LoadInRole(roleID);
        }

        public User GetUser(Guid id)
        {
            return _userRepository.FindEntity(a => a.Id == id);
        }
    }
}
